/*******************************************************************************
 * @brief: systask
 * @Author: aple
 * @Date: 2022-12-04 10:37:03
 * @LastEditors: aple
 * @LastEditTime: 2022-12-07 19:27:41
 * @Verision: 1.0.0.0
 ******************************************************************************/
#include "bsp_api.h"
#include "sys_task.h"
#include "pincfg.h"
#include "mytype.h"
#include "sys_init.h"
#include "nor_cmd.h"
#include "framework.h"

/*****************************************************************************
 * @brief   var
 *****************************************************************************/
sys_run_st_type Gst;

/*****************************************************************************
 * @brief   fun
 *****************************************************************************/
obj_init_app(hal_par_init);

void task_run_arg_init(void);
void check_handle(void);

/*****************************************************************************
 * @brief   system task.
 *****************************************************************************/
void task_system(const void *argument)
{
	thread_create();

	sys_app_init();

	while (1)
	{
		uu32 tick_start, tick_used;

		// 获取CPU计数器
		tick_start = bsp_cpu_clks_get();

		// 脉冲发生器
		sc_pulse_creat(app_tick_get());
		Gst.hal.utick = app_tick_get();

		// 主任务
		sys_app_run_once();

		// rtc
		if (0 == Gst.hal.rtc_ok)
		{
			if (sc_pulse.pulse_1s)
			{
				sig_time_type stim;
				rtc_time_run_sec(&Gst.hal.sim_rtc, 1);
				Gst.hal.rtc_s = time_to_sig(&Gst.hal.sim_rtc);
				Gst.hal.rtc_m = time_rtc_to_micro(&Gst.hal.sim_rtc);
			}
		}
		else
		{
			time_micro_to_rtc(Gst.hal.rtc_m, &Gst.hal.sim_rtc);
		}

		// key
		io_flt_edge(&Gst.key, sc_pulse.pulse_100ms, 2, key.in_st());
		
		if(Gst.key.edge_found)
		{
			dbg_puts("key press\n");
		}
		
		if(Gst.key.edge_lost)
		{
			dbg_puts("key release\n");
		}
		

		// 计算CPU时钟数
		tick_used = bsp_cpu_clks_get() - tick_start;
		if (tick_used > Gst.mix.sys_max_tick)
		{
			Gst.mix.sys_max_tick = tick_used;
			Gst.mix.sys_max_us = bsp_clks_to_us(tick_used);
		}

		if(sc_pulse.pulse_125ms)
		{
			o_led1.tog();
		}
		
		// lib_tick_update(hal_tick_cnt);

		osSignalWait(0, ms_ticks(8));
	}
}

/*******************************************************************************
 * @brief: hal par init
 * @param {*}
 * @return {*}
 ******************************************************************************/
void hal_par_init(void)
{
	uu08 id[8];
	
	memset(id, 0, sizeof(id));
	
	nor_id_read(id);
	
	dbg_u8_print("FLASH ID:" , id, 4, 8);
}


/*******************************************************************************
 * @brief: check handle
 * @param {*}
 * @return {*}
 ******************************************************************************/
int checkx(int arvc, char **argv)
{
	int cnt = 10;
	int flag;
	
	if(arvc > 1)
		cnt = atoi(argv[1]);
	

	// 输出状态
	while (cnt--)
	{
		lib_chk_test();
		
	    flag = lib_chk_err_type();
		
		if(flag)
		{
			dbg_print("flag %d\n", flag);
		}
	}
	
	dbg_print("AHB1: %08X, %08X\n", &RCC->AHB1ENR, RCC->AHB1ENR);
	dbg_print("AHB2: %08X, %08X\n", &RCC->AHB2ENR, RCC->AHB2ENR);
	dbg_print("APB1: %08X, %08X\n", &RCC->APB1LENR, RCC->APB1LENR);
	dbg_print("APB2: %08X, %08X\n", &RCC->APB2ENR, RCC->APB2ENR);
	
	dbg_print("PA CFG: %08X, %08X\n", &GPIOA->MODER, GPIOA->MODER);
	dbg_print("PB CFG: %08X, %08X\n", &GPIOB->MODER, GPIOB->MODER);
	dbg_print("PC CFG: %08X, %08X\n", &GPIOC->MODER, GPIOC->MODER);
	dbg_print("PD CFG: %08X, %08X\n", &GPIOD->MODER, GPIOD->MODER);
	
	return 0;
}
DBG_FUN_EXPORT(checkx);
